﻿/*
 * 由SharpDevelop创建。
 * 用户： abc
 * 日期: 2014/10/22
 * 时间: 17:01
 *
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myProxy
{
    /// <summary>
    /// Description of MultiAddProxyFrom.
    /// </summary>
    ///

    public partial class MultiAddProxyFrom : CCWin.CCSkinMain
    {
        public MultiAddProxyFrom()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }

        public delegate void DelegateAddProxyHandler(object sender, string IPAddress, string port);

        public event DelegateAddProxyHandler AddNewProxy;

        private void ClosebuttonClick(object sender, EventArgs e)
        {
            Dispose();
        }

        private void AddbuttonClick(object sender, EventArgs e)
        {
            ProxyEndPointParser p = new ProxyEndPointParser();
            IPEndPoint ipend;
            foreach (var line in textBox1.Lines)
            {
                ipend = p.GetEndPoint(line);
                if (ipend != null)
                    AddNewProxy?.Invoke(this, ipend.Address.ToString(), ipend.Port.ToString());
            }
        }

        private void MultiAddProxyFrom_Load(object sender, EventArgs e)
        {
        }

        private async void skinButtonGetAddr_Click(object sender, EventArgs e)
        {
            try
            {
                const string baseurl = "http://www.xicidaili.com/wt/";

                const string iplist_xpath = "//*[@class=\"odd\"]";

                List<ProxyStruct> list = new List<ProxyStruct>();

                HttpClient client = new HttpClient();

                TimeSpan timeout = new TimeSpan(0,0,10);

                textBox1.Clear();

                //默认只获取两页
                for (int i = 1; i <= 2; i++)
                {
                    
                    string url = string.Format("{0}{1}",baseurl,i.ToString());

                    Task<System.IO.Stream> getStringTask = client.GetStreamAsync(url);
                    
                    string htmlstr;
                    System.IO.Stream s = await getStringTask;
                    using (System.IO.StreamReader r = new System.IO.StreamReader(s))
                    {
                        htmlstr = r.ReadToEnd();
                    }
                    if (htmlstr.Length > 0)
                    {
                        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                        document.LoadHtml(htmlstr);
                        //获取所有代理服务器ip地址和端口
                        HtmlNodeCollection divList = document.DocumentNode.SelectNodes(iplist_xpath);
                        foreach (var item in divList)
                        {
                            HtmlAgilityPack.HtmlDocument d = new HtmlAgilityPack.HtmlDocument();
                            d.LoadHtml(item.InnerHtml);
                            var tds = d.DocumentNode.SelectNodes("//td");
                            if (tds.Count >= 4)
                            {
                                ProxyStruct info = new ProxyStruct();
                                info.ip = tds[1].InnerText;
                                info.port = tds[2].InnerText;
                                info.name = tds[3].InnerText;
                                list.Add(info);
                            }
                        }
                    }
                }

                
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        textBox1.AppendText(string.Format("cn  {0}  {1}  {2}\n", item.ip, item.port, item.name));
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }
    }

    public class ProxyStruct
    {
        public string ip;
        public string port;
        public string name;
    }
}